import 'package:flutter/material.dart';
import 'package:top_foundation/component/tabbar/controller.dart';

import '../../utils/util.dart';
import 'config.dart';

class TPTabBar extends StatelessWidget {
  const TPTabBar({
    Key? key,
    required this.controller,
    this.selectedStyle,
    this.unSelectedStyle,
    this.height,
    this.backgroundColor,
    this.indicatorColor,
    this.padding = EdgeInsets.zero,
    this.initIndex = 0,
    this.indicatorWidth,
    this.itemBuilder,
  }) : super(key: key);

  final TPTabBarController controller;
  final TextStyle? selectedStyle;
  final TextStyle? unSelectedStyle;
  final double? height;
  final Color? backgroundColor;
  final Color? indicatorColor;
  final EdgeInsets padding;
  final int initIndex;
  final double? indicatorWidth;
  final IndexedWidgetBuilder? itemBuilder;

  List<Widget> _buildItems({
    double? itemWidth,
  }) {
    var ss = selectedStyle ?? TabBarConfig.selectedStyle;
    var unss = unSelectedStyle ?? TabBarConfig.unSelectedStyle;

    return controller.titles
        .asMap()
        .map(
          (key, value) => MapEntry(
            key,
            TPTabItem(
              key: controller.keys[key],
              width: itemWidth,
              title: value,
              style: controller.index.value == key ? ss : unss,
              indicatorColor: indicatorColor,
              indicatorWidth: indicatorWidth,
              isSelected: controller.index.value == key,
              isFixed: controller.scrollController == null,
              onTap: () => controller.actionTabItem(key),
              child:
                  itemBuilder == null ? null : itemBuilder!(Get.context!, key),
            ),
          ),
        )
        .values
        .toList();
  }

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => Container(
        height: height ?? 44.r,
        color: backgroundColor ?? Colors.white,
        alignment: Alignment.centerLeft,
        child: controller.scrollController != null
            ? ListView(
                physics: const ClampingScrollPhysics(),
                scrollDirection: Axis.horizontal,
                controller: controller.scrollController,
                shrinkWrap: true,
                children: _buildItems(),
              )
            : Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: _buildItems(),
              ),
      ),
    );
  }
}

class TPTabItem extends StatelessWidget {
  const TPTabItem({
    Key? key,
    this.title,
    this.style,
    this.indicatorColor,
    this.indicatorWidth,
    required this.onTap,
    this.child,
    this.width,
    this.isSelected = false,
    this.isFixed = true,
  }) : super(key: key);
  final String? title;
  final TextStyle? style;
  final double? width;
  final Widget? child;
  final VoidCallback onTap;
  final bool isSelected;
  final Color? indicatorColor;
  final double? indicatorWidth;
  final bool isFixed;

  Widget _text() {
    return Container(
      alignment: Alignment.center,
      width: width,
      color: Colors.white,
      padding: EdgeInsets.symmetric(horizontal: 10.r),
      child: Text(
        title ?? '',
        style: style,
        maxLines: isFixed ? null : 1,
        textAlign: TextAlign.center,
      ),
    );
  }

  Widget _child() {
    return InkWell(
      onTap: onTap,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          if (child != null)
            child!
          else
            Expanded(
              child: _text(),
            ),
          if (isSelected)
            Container(
              height: 2,
              width: indicatorWidth ?? 20.r,
              color: indicatorColor ?? TabBarConfig.indicatorColor,
            ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    if (isFixed && (width == null || width == 0)) {
      return Expanded(child: _child());
    }
    return _child();
  }
}
